package com.lin.lazybone.fragment;

import android.animation.ObjectAnimator;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

import com.lin.lazybone.R;
import com.lin.lazybone.ui.CourierActivity;
import com.lin.lazybone.ui.GetDeliveryActivity;
import com.lin.lazybone.ui.OrderActivity;
import com.lin.lazybone.ui.OrderListActivity;
import com.lin.lazybone.utils.StaticClass;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by mr.lin on 2017/10/17.
 * 描述：主界面
 */

public class HomeFragment extends Fragment implements View.OnClickListener
{

    //获取viewflipper
    private ViewFlipper viewflipper;
    //存放小点的的集合
    private List<View> mDotList;
    //中间四个按键
    private LinearLayout ji_layout_button;
    private LinearLayout qu_layout_button;
    private LinearLayout cha_layout_button;
    private LinearLayout zhuan_layout_button;

    //图片轮播图片
    private int[] vfImageresId = {R.drawable.pageimage1, R.drawable.pageimage2, R.drawable.pageimage3, R.drawable.pageimage4};

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.fragment_home, null);
        initData();
        initview(view);
        return view;
    }

    //初始化数据
    private void initData()
    {

    }

    //初始化view
    private void initview(View view)
    {
        //图片轮动的小点
        mDotList = new ArrayList<>();
        mDotList.add(view.findViewById(R.id.dot1));
        mDotList.add(view.findViewById(R.id.dot2));
        mDotList.add(view.findViewById(R.id.dot3));
        mDotList.add(view.findViewById(R.id.dot4));
        viewflipper = (ViewFlipper) view.findViewById(R.id.viewflipper);
        //中间四个按钮设置点击事件
        ji_layout_button = (LinearLayout) view.findViewById(R.id.ji_layout_button);
        ji_layout_button.setOnClickListener(this);
        qu_layout_button = (LinearLayout) view.findViewById(R.id.qu_layout_button);
        qu_layout_button.setOnClickListener(this);
        cha_layout_button = (LinearLayout) view.findViewById(R.id.cha_layout_button);
        cha_layout_button.setOnClickListener(this);
        zhuan_layout_button = (LinearLayout) view.findViewById(R.id.zhuan_layout_button);
        zhuan_layout_button.setOnClickListener(this);

        for (int i = 0; i < vfImageresId.length; i++)
        {
            viewflipper.addView(getImageView(vfImageresId[i]));
        }
        viewflipper.setInAnimation(getActivity(), R.anim.left_in);
        viewflipper.setOutAnimation(getActivity(), R.anim.right_out);
        //设置3秒切换
        viewflipper.setFlipInterval(3000);
        //监听viewflipper动画的状态，来实现底部小圆点的跟随动作
        viewflipper.getInAnimation().setAnimationListener(new Animation.AnimationListener()
        {
            @Override
            public void onAnimationStart(Animation animation)
            {
                //切换小圆点的状态
                int current = viewflipper.getDisplayedChild();
                if (current == 0)
                {
                    mDotList.get(mDotList.size() - 1).setBackgroundResource(R.drawable.dot_normal);
                } else
                {
                    mDotList.get(current - 1).setBackgroundResource(R.drawable.dot_normal);
                }
                mDotList.get(current).setBackgroundResource(R.drawable.dot_focused);
            }

            @Override
            public void onAnimationEnd(Animation animation)
            {

            }

            @Override
            public void onAnimationRepeat(Animation animation)
            {

            }
        });
        viewflipper.startFlipping();
    }

    private ImageView getImageView(int imageID)
    {
        ImageView ig = new ImageView(getActivity());
        ig.setBackgroundResource(imageID);
        return ig;
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.ji_layout_button:
                //点击了寄件按钮
                //Toast.makeText(getActivity(), "点击了寄件按钮", Toast.LENGTH_SHORT).show();
                StaticClass.DEL_TYPE = false;
                startActivity(new Intent(getActivity(), GetDeliveryActivity.class));
                break;
            case R.id.qu_layout_button:
                //点击了取件按钮
                //Toast.makeText(getActivity(), "点击了取件按钮", Toast.LENGTH_SHORT).show();
                StaticClass.DEL_TYPE = true;
                startActivity(new Intent(getActivity(), GetDeliveryActivity.class));
                break;
            case R.id.cha_layout_button:
                //点击了查件按钮
//                Toast.makeText(getActivity(), "点击了查件按钮", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(getActivity(), CourierActivity.class));
                break;
            case R.id.zhuan_layout_button:
                //点击了赚钱按钮
                //Toast.makeText(getActivity(), "点击了赚钱按钮", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(getActivity(), OrderListActivity.class));
                break;
        }
    }
}
